package fr.upec.tisdtr.indoorlocation.hardware;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class ConnectionManager {
	
	private static final String TAG = "ConnectionManager";
	
	public static final int CONNECTIVITY_NONE = -1;
	
	public static int getCurrentNetworkType(Context context) {
		Log.v(TAG, "getCurrentNetworkType");
		int result = CONNECTIVITY_NONE;
		String display = "connectivity_none";
		if(context != null) {
			try {
				ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
				NetworkInfo info = (connectivity != null ? connectivity.getActiveNetworkInfo() : null);
				if (info != null) {
					display = info.getTypeName();
					result = info.getType();
				}
			} catch (java.lang.NullPointerException e) {
				Log.w(TAG, "getCurrentNetworkType.NullPointerException : " + e);
			}
		}
		Log.v(TAG, "activeNetworkType: " + display);
		return result;
	}
	
	public static boolean isMobileActiveConnectionType(Context context) {
		boolean value = getCurrentNetworkType(context) == ConnectivityManager.TYPE_MOBILE;
		Log.v(TAG, "isMobileActiveConnectionType : "+value);
		return value;
	}
	
	public static boolean isNoneCurrentConnectionType(Context context) {
		boolean value = getCurrentNetworkType(context) == CONNECTIVITY_NONE;
		Log.v(TAG, "isNoneCurrentConnectionType : "+value);
		return value;
	}

}
